package org.ants.tinymvc.service;

import java.util.List;
import java.util.Map;

import org.ants.tinymvc.dao.BaseTinyDbDao;
import org.ants.tinymvc.entity.BaseEntity;
import org.tinygroup.logger.Logger;
import org.tinygroup.logger.LoggerFactory;
import org.tinygroup.tinydb.Bean;
import org.tinygroup.tinydb.Pager;
import org.tinygroup.tinydb.exception.TinyDbException;
import org.tinygroup.tinydb.order.OrderBean;
import org.tinygroup.tinydb.query.Conditions;

public abstract class BaseTinyDbService<T extends BaseEntity> {
	protected static Logger logger = LoggerFactory.getLogger(BaseTinyDbService.class);
    
    /**
     * 在子类实现此函数,为下面的CRUD操作提供DAO.
     */
    protected abstract BaseTinyDbDao<T> getEntityDao();
	/**
	 * 根据实体添加数据库
	 * @param t
	 * @return
	 * @throws Exception
	 */
	public  BaseEntity insert(T t) throws Exception{
		return this.getEntityDao().insert(t);
	}

	/**
	 * 通过Bean添加数据库
	 * @param bean
	 * @return
	 */
	public  Bean insert(Bean bean){
		return this.getEntityDao().insert(bean);
	}

	/**
	 * 通过实体修改数据库
	 * @param t
	 * @return
	 * @throws Exception
	 */
	public  int update(T t) throws Exception{
		return this.getEntityDao().update(t);
	}

	/**
	 * 通过Bean修改数据库
	 * @param t
	 * @return
	 * @throws Exception
	 */
	public  int update(Bean bean) throws Exception{
		return this.getEntityDao().update(bean);
	}

	/**
	 * key:删除的主键内容  flag:true 硬删除 false:软删除
	 * @throws Exception 
	 */
	public  int deleteByKey(String key, boolean flag) throws Exception{
		return this.getEntityDao().deleteByKey(key, flag);
	}

	/**
	 * 硬删除
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public  int deleteByKey(String id) throws Exception{
		return this.getEntityDao().deleteByKey(id);
	}

	public  int deleteByKeys(String... keys) throws Exception{
		return this.getEntityDao().deleteByKeys(keys);
	}

	/**
	 * 
	 * @param flag true 硬删除 false 软删除
	 * @param keys  主键信息
	 * @return
	 * @throws Exception
	 */
	public  int deleteByKeys(boolean flag, String... keys)
			throws Exception{
		return this.getEntityDao().deleteByKeys(flag, keys);
	}

	public  int deleteBeans(Bean... beans) throws Exception{
		return this.getEntityDao().deleteBeans(beans);
	}

	public  int deleteBeans(boolean flag, Bean... beans)
			throws Exception{
		return this.getEntityDao().deleteBeans(flag, beans);
	}

	public  T findObjectByKey(String key, T t) throws Exception{
		return this.getEntityDao().findObjectByKey(key, t);
	}

	public  Bean findBeanByKey(String key) throws Exception{
		return this.getEntityDao().findBeanByKey(key);
	}
	
	public Bean[] findBeans(Bean bean){
		return this.getEntityDao().findBeans(bean);
	}

	/**
	 * 根据主键ID查询并上锁
	 * @param id
	 * @return
	 */
	public  Bean findByPkWithLock(String id){
		return this.getEntityDao().findByPkWithLock(id);
	}

	/**
	 * 查询所有的记录
	 * @return
	 */
	public  List<Bean> findAll(){
		return this.getEntityDao().findAll();
	}

	/**
	 * 根据条件查询
	 * @param qc
	 * @param orderBeans
	 * @return
	 */
	public  List<Bean> findByCondition(Conditions qc,
			OrderBean[] orderBeans){
		return this.getEntityDao().findByCondition(qc, orderBeans);
	}

	/**
	 * 根据条件查询单个实体
	 * @param qc
	 * @param orderBeans
	 * @return
	 */
	public  Bean findSigleByCondition(Conditions qc){
		return this.getEntityDao().findSigleByCondition(qc);
	}

	/**
	 * 根据条件查询单个实体
	 * @param qc
	 * @param orderBeans
	 * @return
	 */
	public  T findSingleByCondition(Conditions qc, T t){
		return this.getEntityDao().findSingleByCondition(qc, t);
	}

	/**直接使用SQL查询
	 * 
	 */
	public  List<Bean> findForList(String sql){
		return this.getEntityDao().findForList(sql);
	}

	/**
	 * SQL查询后返回实体
	 * @param sql
	 * @param entity
	 * @return
	 */
	public  List<T> findForList(String sql, T entity){
		return this.getEntityDao().findForList(sql, entity);
	}

	/**
	 * 直接获取前几个
	 * @param qc
	 * @param begin
	 * @param end
	 * @return
	 */
	public  List<Bean> findForLimitList(Conditions qc,
			OrderBean[] orderBean, int begin, int end){
		return this.getEntityDao().findForLimitList(qc, begin, end);
	}

	public  List<Bean> findForLimitList(Conditions qc, int begin,
			int end){
		return this.getEntityDao().findForLimitList(qc, begin, end);
	}

	public  Bean[] batchInsertBeans(List<Bean> beans) throws Exception{
		return this.getEntityDao().batchInsertBeans(beans);
	}

	public  Bean[] batchInsertBeans(List<Bean> beans, int batchSize)
			throws Exception{
		return this.getEntityDao().batchInsertBeans(beans,batchSize);
	}

	public  Bean[] batchInsert(List<T> entitys, int batchSize)
			throws Exception{
		return this.getEntityDao().batchInsert(entitys, batchSize);
	}

	public  Bean[] batchInsert(List<T> entitys) throws Exception{
		return this.getEntityDao().batchInsert(entitys);
	}

	public  int[] batchUpdate(List<T> entitys) throws Exception{
		return this.getEntityDao().batchUpdate(entitys);
	}

	public  void batchUpdate(List<T> entitys, int batchSize)
			throws Exception{
		this.getEntityDao().batchUpdate(entitys, batchSize);
	}

	public  int[] batchUpdateBeans(Bean[] beans){
		return this.getEntityDao().batchUpdateBeans(beans);
	}

	public  Bean[] batchUpdateBeans(List<Bean> beans, int batchSize){
		return this.getEntityDao().batchUpdateBeans(beans, batchSize);
	}

	public  int[] batchDelete(List<T> entitys) throws Exception{
		return this.getEntityDao().batchDelete(entitys);
	}

	public  void batchDelete(List<T> entitys, int batchSize)
			throws Exception{
		this.getEntityDao().batchDelete(entitys, batchSize);
	}

	public  int[] batchDeleteBean(List<Bean> beans){
		return this.getEntityDao().batchDeleteBean(beans);
	}

	public  void batchDeleteBean(List<Bean> beans, int batchSize){
		this.getEntityDao().batchDeleteBean(beans,batchSize);
	}

	//查询总记录数
	public  int account(String sql, Object... parameters)
			throws TinyDbException{
		return this.getEntityDao().account(sql, parameters);
	}

	public  int account(String sql, List<Object> parameters)
			throws TinyDbException{
		return this.getEntityDao().account(sql, parameters);
	}

	public  int account(String sql, Map<String, Object> parameters)
			throws TinyDbException{
		return this.getEntityDao().account(sql, parameters);
	}

	/**
	 * 根据bean对象查询该bean对象的记录数
	 *
	 * @param bean
	 * @return
	 */
	public  int account(Bean bean) throws TinyDbException{
		return this.getEntityDao().account(bean);
	}

	//返回分页结果的分页接口
	public  Pager getPager(String sql, int start, int limit)
			throws TinyDbException{
		return this.getEntityDao().getPager(sql, start, limit);
	}

	public  Pager getPager(String sql, int start, int limit,
			Object... parameters) throws TinyDbException{
		return this.getEntityDao().getPager(sql, start, limit, parameters);
	}

	public  Pager getPager(String sql, int start, int limit,
			List<Object> parameters) throws TinyDbException{
		return this.getEntityDao().getPager(sql, start, limit, parameters);
	}

	public  Pager getPager(String sql, int start, int limit,
			Map<String, Object> parameters) throws TinyDbException{
		return this.getEntityDao().getPager(sql, start, limit, parameters);
	}

	/**
	 * 分页查询bean
	 *
	 * @param bean
	 * @param start
	 * @param limit
	 * @return
	 */
	public  Bean[] getBeans(Bean bean, int start, int limit)
			throws TinyDbException{
		return this.getEntityDao().getBeans(bean, start, limit);
	}

	/**
	 * 分页查询bean
	 *
	 * @param bean
	 * @param start
	 * @param limit
	 * @return
	 */
	public  Pager getPager(Bean bean, int start, int limit)
			throws TinyDbException{
		return this.getEntityDao().getPager(bean, start, limit);
	}
}
